//
//  YJDButton.m
//  YJDSDK
//
//  Created by skyline on 2018/7/26.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "YJDButton.h"
@interface YJDButton()
@property (nonatomic, strong) YJDImageView *IMG;
@property (nonatomic, strong) YJDLabel *titleLBL;
@end
@implementation YJDButton


- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.layer.masksToBounds = YES;
        self.style = YJDPlatformStyleDefault;
        self.backgroundColor = ([YJDHelper shareHelp].style == YJDPlatformStyleDefault)?[YJDHelper shareHelp].color:[UIColor clearColor];
    }
    return self;
}

- (YJDImageView *)IMG
{
    if (!_IMG)
    {
        _IMG = [[YJDImageView alloc] init];
        [self addSubview:_IMG];
        [_IMG mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsZero);
        }];
    }
    return _IMG;
}

- (YJDLabel *)titleLBL
{
    if (!_titleLBL)
    {
        _titleLBL = [[YJDLabel alloc] init];
        [self addSubview:_titleLBL];
        [_titleLBL mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.center.equalTo(self);
        }];
    }
    return _titleLBL;
}

- (void)setStyle:(YJDButtonTitleStyle)style
{
    if (_style != style)
    {
        _style = style;
        self.layer.cornerRadius = (_style == YJDButtonTitleStyleDafault)?5.0:0.0;
        self.titleLBL.textColor = (_style == YJDButtonTitleStyleDafault)?[UIColor whiteColor]:[YJDHelper shareHelp].color;
        self.backgroundColor = (_style == YJDButtonTitleStyleDafault)?self.backgroundColor:[UIColor clearColor];
    }
}

- (void)setContent:(NSString *)content
{
    if (_content != content)
    {
        _content = content;
        if ([_content yjd_path])
        {
            self.IMG.imageName = _content;
            if (self.IMG.image)
            {
                self.bounds = CGRectMake(0, 0, self.IMG.image.size.width * self.height / self.IMG.image.size.height, self.height);
                [self mas_updateConstraints:^(SK_MASConstraintMaker *make) {
                    make.size.mas_offset(self.bounds.size);
                }];
            }
        }
        else
        {
            self.titleLBL.text = _content;
            if (self.style == YJDButtonTitleStyleCameo)
            {
                [self mas_updateConstraints:^(SK_MASConstraintMaker *make) {
                    make.size.mas_offset(self.titleLBL.bounds.size);
                }];
            }
        }
    }
}

- (void)setTitleAttributed:(NSAttributedString *)titleAttributed
{
    if (_titleAttributed != titleAttributed)
    {
        _titleAttributed = titleAttributed;
        self.titleLBL.attributedText = _titleAttributed;
        [self mas_updateConstraints:^(SK_MASConstraintMaker *make) {
            make.size.mas_equalTo(self.titleLBL.bounds.size);
        }];
    }
}

- (void)setSelected:(BOOL)selected
{
    [super setSelected:selected];
    self.alpha = (selected)?1.0:0.5;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    self.alpha = 0.5;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    self.alpha = 1.0;
}


/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
